{% extends 'layout.html' %}

{% block content %}
    <div class="container">
        <div style="margin-bottom: 10px">
            <input type="button" value="新建订单1" class="btn btn-success" data-toggle="modal"
                   data-target="#myModal">
            <input id="btnAdd" type="button" value="新建订单2" class="btn btn-success">
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
                订单列表
            </div>

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>订单号</th>
                    <th>名称</th>
                    <th>价格</th>
                    <th>状态</th>
                    <th>管理员</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for obj in queryset %}
                    <tr>
                        <th>{{ obj.id }}</th>
                        <th>{{ obj.oid }}</th>
                        <td>{{ obj.title }}</td>
                        <td>{{ obj.price }}</td>
                        <td>{{ obj.get_status_display }}</td>
                        <td>{{ obj.admin.username }}</td>
                        <td>
                            <input uid="{{ obj.id }}" type="button" class="btn btn-warning btn-xs btn-edit"
                                   value="编辑">
                            <input uid="{{ obj.id }}" type="button" class="btn btn-danger btn-xs btn-delete"
                                   value="删除">
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <div class="clearfix">
            <ul class="pagination" style="float: left">
                {{ page_str }}
            </ul>
        </div>
    </div>

    <!-- 新建订单模态框 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel"></h4>
                </div>
                <div class="modal-body">
                    <form id="FormAdd">
                        <div class="clearfix">
                            {% for field in form %}
                                <div class="col-xs-6">
                                    <div class="form-group" style="position: relative; margin-bottom: 20px">
                                        <label>{{ field.label }}</label>
                                        {{ field }}
                                        <span class="error-msg" style="color: red; position: absolute"></span>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                    <button id="btnSave" type="button" class="btn btn-primary">保 存</button>
                </div>
            </div>
        </div>
    </div>
    <!-- 删除订单模态框 -->
    <!-- Modal -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="alert alert-danger alert-dismissible fade in" role="alert">
                <h4>是否确认删除？</h4>
                <p style="margin-top: 10px">删除后所有相关数据都会被删除，请谨慎操作！</p>
                <p style="text-align:right;">
                    <button id="btnConfirmDelete" type="button" class="btn btn-danger">确 定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                </p>
            </div>
        </div>
    </div>

{% endblock %}

{% block js %}
    <script type="text/javascript">
        var DELETE_ID;
        var EDIT_ID;
        $(function () {
            binBtnAddEvent();
            bindBtnSaveEvent();
            bindBtnDeleteEvent();
            bindBtnConfirmDeleteEvent();
            bindBtnEditEvent();
        })

        function binBtnAddEvent() {
            // 绑定新建订单按钮点击事件
            $("#btnAdd").click(function () {
                EDIT_ID = undefined;
                $("#FormAdd")[0].reset();
                $("#myModalLabel").text("新建")
                // 显示模态框
                $("#myModal").modal("show");
            });
        }

        function bindBtnSaveEvent() {
            $("#btnSave").click(function () {
                $('.error-msg').empty();
                if (EDIT_ID) {
                    doEdit()
                } else {
                    doAdd()
                }

            })
        }

        function doAdd() {
            $.ajax({
                url: '/order/add/',
                type: 'post',
                data: $("#FormAdd").serialize(),
                dataType: 'JSON',
                success: function (res) {
                    if (res.status) {
                        // 清空表单内容
                        //$("#FormAdd")是jQery对象 --> $("#FormAdd")[0]是DOM对象
                        $("#FormAdd")[0].reset();
                        // 关闭模态框
                        $("#myModal").modal("hide");
                        // 刷新页面
                        location.reload();
                    } else {
                        $.each(res.error, function (name, errorList) {
                            $("#id_" + name).next().text(errorList[0]);
                        })
                    }
                }
            })
        }

        function doEdit() {
            $.ajax({
                url: '/order/detail/' + '?uid=' + EDIT_ID,
                type: 'post',
                data: $("#FormAdd").serialize(),
                dataType: 'JSON',
                success: function (res) {
                    if (res.status) {
                        // 清空表单内容
                        //$("#FormAdd")是jQery对象 --> $("#FormAdd")[0]是DOM对象
                        $("#FormAdd")[0].reset();
                        // 关闭模态框
                        $("#myModal").modal("hide");
                        // 刷新页面
                        location.reload();
                    } else {
                        if (res.tips) {
                            alert(res.tips);
                        } else {
                            $.each(res.error, function (name, errorList) {
                                $("#id_" + name).next().text(errorList[0]);
                            })
                        }
                    }
                }
            })

        }


        function bindBtnDeleteEvent() {
            $(".btn-delete").click(function () {
                // 显示删除确认框
                $("#deleteModal").modal("show");
                // 获取当前行的ID并赋值给全局变量DELETE_ID
                DELETE_ID = $(this).attr("uid");
            })
        }

        function bindBtnConfirmDeleteEvent() {
            $("#btnConfirmDelete").click(function () {
                // 将当前行的ID传递给后台进行删除
                $.ajax({
                    url: '/order/delete/',
                    type: 'get',
                    data: {
                        uid: DELETE_ID
                    },
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            // 隐藏删除确认框
                            $("#deleteModal").modal("hide");
                            // 刷新页面
                            location.reload();
                            // 要删除的id置为0
                            DELETE_ID = undefined;
                        } else
                            alert(res.error)
                    }
                })
            })
        }

        function bindBtnEditEvent() {
            $(".btn-edit").click(function () {
                $("#FormAdd")[0].reset();
                var uid = $(this).attr("uid");
                // 将当前行的ID传递给全局变量EDIT_ID
                EDIT_ID = uid;
                // 发送ajax去后端获取相应行的数据
                $.ajax({
                    url: '/order/edit/',
                    type: 'get',
                    data: {
                        uid: uid
                    },
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            console.log(res.data);
                            // 将获取到的数据填充到模态框中
                            $.each(res.data, function (name, value) {
                                $("#id_" + name).val(value);
                            })
                            // 获取当前行的ID
                            $("#myModalLabel").text("编辑")
                            $('#myModal').modal('show')
                        } else {
                            alert(res.error)
                        }
                    }
                })
            })
        }


    </script>
{% endblock %}